/*
 * Spring 2012 TCSS360 Deliverable 3
 * Otis Alexander
 */

package model;

import java.math.BigDecimal;

import exceptions.BidLessThanMinBidException;
import exceptions.BidderNotRegisteredException;
import exceptions.DuplicateBidException;

public class Item implements Comparable<Item> {

	private String my_itemName;

	private int my_quantity;

	private BigDecimal my_minBid;

	private String my_condition;

	private String my_comment;

	private String my_npo;

	private int my_ID;

	private String my_unique_ID;

	public Item(final String itemName, final int quantity,
			final BigDecimal minBid,
			final String condition, final String comment,
			final String npo, final int ID) {
		my_itemName = itemName;
		my_quantity = quantity;
		my_minBid = minBid;
		my_condition = condition;
		my_comment = comment;
		my_npo = npo;
		my_ID = ID;
		my_unique_ID = npo + "_" + Integer.toString(ID);
	}

	/**
	 * Places a bid on an item, stored on the bidder.
	 * 
	 * @pre bidAmount != null
	 * @pre bidder != null
	 * @pre bidder.getCreditCard() == String.length() > 0
	 */
	public void placeBid(final BigDecimal bidAmount,
			final Bidder bidder)
			throws BidLessThanMinBidException,
			BidderNotRegisteredException,
			DuplicateBidException {
		if (bidder.getCreditCard() == null) {
			throw new BidderNotRegisteredException(
					"Bidder needs to register a credit card",
					"No Credit Card");
		} else if (bidder.getCreditCard().equals("")) {
			throw new BidderNotRegisteredException(
					"Bidder needs to register a credit card",
					"No Credit Card");
		}
		if (bidAmount.compareTo(my_minBid) == -1)
			throw new BidLessThanMinBidException(
					"The minimum bid for this item is set at "
							+ "$" + my_minBid, "Bad Bid");
		if (bidder.alreadyBidded(this)) {
			throw new DuplicateBidException();
		}
		bidder.addBid(this, bidAmount);
	}

	public String getItemName() {
		return my_itemName;
	}

	public int getQuantity() {
		return my_quantity;
	}

	public BigDecimal getMinBid() {
		return my_minBid;
	}

	public String getCondition() {
		return my_condition;
	}

	public String getComment() {
		return my_comment;
	}

	public String getNPO() {
		return my_npo;
	}

	public int getID() {
		return my_ID;
	}

	public String get_unique_id() {
		return my_unique_ID;
	}

	@Override
	public int compareTo(final Item the_item) {
		int result = 0;

		if (my_ID > the_item.my_ID) {
			result = 1;
		} else if (my_ID < the_item.my_ID) {
			result = -1;
		}

		return result;
	}
}
